package com.emuporium.gymrat.server.data;

import java.util.LinkedHashMap;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;

@Root
public class Entry implements Comparable<Entry> {

	@Element
	private String exercise;
	@Element
	private int exerciseId;
	@Element
	private String muscleGroup;
	@Attribute
	private int id;
	@ElementMap(entry="variation", key="id", attribute=true, inline=true)
	private LinkedHashMap<Integer, String> variations;
	@Element
	private int order=0;
	@Element(required=false)
	private int sets;
	@Element(required=false)
	private int reps;
	@Element(required=false)
	private String note;

	public Entry() {
		variations = new LinkedHashMap<Integer, String>();
	}
	
	public void addVariation(int id, String _variation) {
		if(!variations.containsKey(id))
			variations.put(id, _variation);
	}

	public LinkedHashMap<Integer,String> getVariations() {
		return variations;
	}

	public void setVariations(LinkedHashMap<Integer,String> variations) {
		this.variations = variations;
	}

	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public String getExercise() {
		return exercise;
	}

	public void setExercise(String exercise) {
		this.exercise = exercise;
	}
	
	public String getMuscleGroup(){
		return muscleGroup;
	}
	
	public void setMuscleGroup(String muscleGroup){
		this.muscleGroup = muscleGroup;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		if(order != 0){
			sb.append(""+order+") ");
		}

		sb.append(exercise);
		if (variations.size() > 0)
			sb.append(" (");
		for (String v : variations.values()) {
			sb.append(v + ",");
		}

		int comma = sb.lastIndexOf(",");
		if (comma > 0)
			sb.replace(comma, comma + 1, ")");

		return sb.toString();
	}

	@Override
	public int compareTo(Entry other) {
	    final int BEFORE = -1;
	    final int EQUAL = 0;
	    final int AFTER = 1;
	    
	    if (this == other)
	    	return EQUAL;
	    
	    if(this.order < other.order)
	    	return BEFORE;
	    if(this.order > other.order)
	    	return AFTER;
	    
	    //TODO: optional/alternate logic goes here
	    
	    return EQUAL;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getSets() {
		return sets;
	}

	public void setSets(int sets) {
		this.sets = sets;
	}

	public int getReps() {
		return reps;
	}

	public void setReps(int reps) {
		this.reps = reps;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public int getExerciseId() {
		return exerciseId;
	}

	public void setExerciseId(int exerciseId) {
		this.exerciseId = exerciseId;
	}

}
